<?php
/**
+--------------------------------------------------------------
 * User: maqun
+--------------------------------------------------------------
 * Date: 2015/11/28
+--------------------------------------------------------------
 * Time: 15:36
+--------------------------------------------------------------
 *  题目列表
+--------------------------------------------------------------
 */
class problemAction extends BaseAction {

    protected $page_size = 20; //列表每页显示条数


    public function  _initialize(){
        self::$action = 2;
        parent::_initialize();
    }

    /**
     * 首页列表显示
     */
    public function index(){
        
        $problem_id = !empty($_GET['problem_id']) ? $this->filterId($_GET['problem_id']) : '';
        $keywords   = !empty($_GET['keywords']) ? $_GET['keywords'] : '';
        $difficulty = (isset($_GET['difficulty'])&& -1!==intval($_GET['difficulty']))? intval($_GET['difficulty']) : -1;
        $tag        = !empty($_GET['tag']) ? intval($_GET['tag']) : 0;
        $tag_id = D('ProblemTag')->getListByTag($tag); // 标签

        $table = ' problem as a';
        $fields= ' a.* ';
        $where = ' 1 ';
        $group = ' group by a.problem_id ';
        $order = ' order by a.problem_id ASC ';
        if(!empty($keywords)){
            $where .= " and locate('".$keywords."',a.title) ";
        }
        if(-1!==$difficulty){
            $where .= " and a.difficult=".$difficulty." ";
        }
        if(!empty($problem_id)){
            $where .= " and a.problem_id in(".$problem_id.") ";
        }
        if(!empty($tag)){
            $where .= " and a.problem_id in(".$tag_id.") ";
        }
        $sql_total = "select count(DISTINCT (problem_id)) as total from {$table} where {$where}";
        $res = D('Problem')->query($sql_total);
        $count = !empty($res) ? $res[0]['total']:0;

        vendor('Page');
//      import("ORG.Util.Page");
        $page = new Page($count,$this->page_size);

        $sql   = "select {$fields} from {$table} where {$where} {$group} {$order} limit ".$page->firstRow.",".$page->listRows;
        $list = D('Problem')->query($sql);
        //获取分类标签
        $tags = D('ProblemTag')->field('id,tag')->where("1 and problemIdList is not null ")->select();
        $this->assign('tag',$tag);//当前点击的标签
        $this->assign('tags',$tags);//所有问题id不为空的标签

        $this->assign('list',$list);
        $this->assign('problem_id',$problem_id);
        $this->assign('keywords',$keywords);
        $this->assign('difficulty',$difficulty);
        $this->assign('page',$page->show());
        $this->display('index');
    }

    /**
     * 问题详情查看
     */
    public function viewProblem(){
        $id = !empty($_GET['id']) ? intval($_GET['id']):0;
        if(0===$id){
            $this->error('id错误！');
        }
        $info = D('Problem')->where('problem_id='.$id)->select();
        if(empty($info)){
            $this->error('访问的数据不存在！');
        }
        $this->assign('info',$info);
        $this->display('view');
    }

}